using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Collections.Generic;

/// <summary>
/// Summary description for CategoryDao
/// </summary>
public class CategoryDao
{
    public CategoryDao() { }

    public DataTable LoadAll()
    {
        string proc = "sp_LoadAllCategories";
        DataSet ds = DbProvider.LoadData(proc);
        return ds.Tables[0];
    }

    public DataTable LoadCategories(decimal parentID)
    {
        string proc = "sp_LoadCategories";
        SqlParameter p = new SqlParameter("@ParentID", SqlDbType.Decimal);
        p.Value = parentID;
        DataSet ds = DbProvider.LoadData(proc, p);
        return ds.Tables[0];
    }

    public List<Category> GetListByParentID(decimal parentID)
    {
        DataTable dt = LoadCategories(parentID);
        if (dt.Rows.Count == 0) return null;

        List<Category> categories = new List<Category>();
        foreach (DataRow row in dt.Rows)
        {
            decimal catID = Convert.ToDecimal(row["CatID"]);
            string catName = row["CatName"].ToString();
            bool leaf = Convert.ToBoolean(row["Leaf"]);
            categories.Add(new Category(catID, catName, parentID, leaf));
        }

        dt.Dispose();
        return categories;
    }

    public DataTable LoadLeafCategories()
    {
        string proc = "sp_LoadLeafCategories";
        DataSet ds = DbProvider.LoadData(proc);
        return ds.Tables[0];
    }

    public bool Delete(decimal catID, ref string err)
    {
        string proc = "sp_DelCategory";
        SqlCommand cmd = DbProvider.CreateCommand(proc);
        cmd.Parameters.Add("@CatID", SqlDbType.Decimal);
        cmd.Parameters.Add("@err", SqlDbType.NVarChar).Direction = ParameterDirection.Output;
        cmd.Parameters.Add("@rs", SqlDbType.Bit).Direction = ParameterDirection.Output;

        cmd.Parameters["@CatID"].Value = catID;
        cmd.Parameters["@err"].Size = 255;

        DbProvider.ExecCmd(cmd);
        bool rs = (bool)cmd.Parameters["@rs"].Value;
        if (!rs) err = cmd.Parameters["@err"].Value.ToString();

        return rs;
    }

    public void Update(Category dto)
    {
        string proc = "sp_UpdCategory";
        SqlCommand cmd = DbProvider.CreateCommand(proc);
        cmd.Parameters.Add("@CatID", SqlDbType.Decimal);
        cmd.Parameters.Add("@CatName", SqlDbType.NVarChar);

        cmd.Parameters["@CatID"].Value = dto.CatID;
        cmd.Parameters["@CatName"].Value = dto.CatName;
        DbProvider.ExecCmd(cmd);
    }

    public bool Add(Category dto)
    {
        string proc = "sp_AddCategory";
        SqlCommand cmd = DbProvider.CreateCommand(proc);

        cmd.Parameters.Add("@CatName", SqlDbType.NVarChar);
        cmd.Parameters.Add("@ParentID", SqlDbType.Decimal);
        cmd.Parameters.Add("@CatID", SqlDbType.Decimal).Direction = ParameterDirection.Output;
        cmd.Parameters.Add("@rs", SqlDbType.Bit).Direction = ParameterDirection.Output;

        cmd.Parameters["@CatName"].Value = dto.CatName;
        cmd.Parameters["@ParentID"].Value = dto.ParentID;

        DbProvider.ExecCmd(cmd);
        bool rs = (bool)cmd.Parameters["@rs"].Value;
        if (rs) dto.CatID = (decimal)cmd.Parameters["@CatID"].Value;
        return rs;
    }
}
